<template>
  <div class="app-container">
    <el-card shadow="hover">
      <div>
        <i class="el-icon-tickets"></i>
        <span> 资讯百科分类</span>
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAddCate"
          style="margin-left: 20px;"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multiple"
          @click="handleDelete"
          >批量删除</el-button
        >
      </div>
      <el-table
        v-loading="loading"
        :data="cateList"
        @selection-change="handleSelectionChange"
        style="margin-top: 10px;"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="70" align="center" />
        <el-table-column
          label="资讯百科分类"
          prop="classifyName"
          align="center"
          width="200"
        ></el-table-column>
        <el-table-column
          label="创建时间"
          align="center"
          prop="createTime"
          width="150"
        >
          <template slot-scope="scope">
            <span>{{ formatT(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="200"
        >
          <template slot-scope="scope">
            <el-button
              size="small"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdateCate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="small"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-card shadow="hover" style="margin-top: 20px;">
      <div>
        <i class="el-icon-tickets"></i>
        <span> 资讯百科文章</span>
        <el-select class="select" v-model="queryParams.classifyId" placeholder="请选择文章分类" clearable @change="handleQuery">
          <el-option
            v-for="item in cateList"
            :key="item.id"
            :label="item.classifyName"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-button
          icon="el-icon-search"
          type="cyan"
          size="small"
          style="margin-left: 20px;"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="small"
          @click="handleAdd"
          >新增</el-button
        >
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="small"
          :disabled="multipleList"
          @click="handleDeleteNews"
          >批量删除</el-button
        >
      </div>
      <el-table
        v-loading="loading1"
        :data="newsList"
        @selection-change="handleSelectionChange1"
        border
        style="margin-top: 10px;"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column type="index" label="序号" width="70" align="center" />
        <el-table-column
          label="资讯文章标题"
          prop="consultTitle"
          align="center"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="文章分类" align="center" width="150">
          <template slot-scope="scope">
            {{ getClassifyName(scope.row.classifyId) }}
          </template>
        </el-table-column>
        <el-table-column label="资讯文章图片" align="center" min-width="160">
          <template slot-scope="scope">
            <img style="height: 80px" :src="showUrl + scope.row.logo" />
          </template>
        </el-table-column>
        <el-table-column
          label="文章简述"
          prop="sketch"
          align="center"
          width="200"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          label="操作"
          align="center"
          min-width="200"
          fixed="right"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            >
            <el-button
              size="mini"
              type="danger"
              icon="el-icon-delete"
              @click="handleDeleteNews(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="newsTotal > 0"
        :total="newsTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加/编辑 资讯百科分类 -->
    <el-dialog :title="cateTitle" :visible.sync="cateOpen" width="600px">
      <el-form
        ref="cateForm"
        :model="cateForm"
        :rules="cateRules"
        label-width="120px"
      >
        <el-form-item label="资讯百科类名：" prop="classifyName">
          <el-input v-model="cateForm.classifyName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitCateForm">确 定</el-button>
        <el-button @click="cancel1">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改 资讯文章 -->
    <el-dialog :title="title" :visible.sync="open" width="65%">
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="文章标题:" prop="title">
              <el-input v-model="form.consultTitle" />
            </el-form-item>
            <el-form-item label="seo标题:" prop="title">
              <el-input v-model="form.title" />
            </el-form-item>
            <el-form-item label="keywords关键词:" prop="keywords">
              <el-input v-model="form.keywords" />
            </el-form-item>
            <el-form-item label="description描述:" prop="description">
              <el-input v-model="form.description" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文章分类:" prop="classifyId">
              <el-select
                v-model="form.classifyId"
                placeholder="请选择文章所属资讯分类"
              >
                <el-option
                  v-for="item in cateList"
                  :key="item.id"
                  :label="item.classifyName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="上传图片:" prop="logo">
              <el-upload
                class="avatar-uploader"
                :action="upImage + '/cms/company/upload'"
                :show-file-list="false"
                :headers="headers"
                :on-success="handleAvatarSuccess"
              >
                <img
                  v-if="form.logo"
                  :src="showUrl + form.logo"
                  class="avatar"
                />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文章简述:" prop="sketch">
              <el-input
                type="textarea"
                :autosize="{ minRows: 2 }"
                placeholder="请输入内容"
                v-model="form.sketch"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文章内容:" prop="context">
              <Editor v-model="form.context" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitNewsForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryNewsCate,
  addNewsCate,
  updateNewsCate,
  delNewsCate,
  queryNews,
  addNews,
  updateNews,
  delNews
} from "@/api/web/index";
import Editor from "@/components/Editor";
import { getToken } from "@/utils/auth";

export default {
  name: "Information",
  components: {
    Editor
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      upImage: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      loading1: true,
      // 选中数组
      ids: [],
      all: [],
      // 非单个禁用
      single: true,
      singleList: true,
      // 非多个禁用
      multiple: true,
      multipleList: true,
      // 总条数
      total: 0,
      newsTotal: 0,
      // 资讯百科分类数据
      cateList: [],
      cateTitle: "",
      cateOpen: false,
      cateForm: {},
      cateRules: {
        classifyName: [
          { required: true, message: "资讯百科类名不能为空", trigger: "blur" }
        ]
      },

      // 查询参数
      queryParams: {
        type:1,
        pageNum: 1,
        pageSize: 10,
        classifyId: undefined
      },
      newsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        consultTitle: [
          { required: true, message: "资讯文章标题不能为空", trigger: "blur" }
        ],
        classifyId: [
          { required: true, message: "文章所属类型不能为空", trigger: "blur" }
        ],
        logo: [
          { required: true, message: "资讯图片不能为空", trigger: "blur" }
        ],
        sketch: [
          { required: true, message: "文章简述不能为空", trigger: "blur" }
        ],
        context: [
          { required: true, message: "文章内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getCateList();
    this.getList();
  },
  methods: {
    // 查询资讯百科分类
    getCateList() {
      this.loading = true;
      queryNewsCate(this.queryParams).then(res => {
        // console.log(res);
        this.cateList = res.rows;
        this.total = res.total;
        this.loading = false;
      });
    },
    // 点击新增分类
    handleAddCate() {
      if (this.total < 3) {
        this.cateForm = {};
        this.cateOpen = true;
        this.cateTitle = "新增分类";
      } else {
        return this.msgError("资讯百科分类不能超过3项！");
      }
    },
    // 点击编辑分类
    handleUpdateCate(row) {
      console.log(row,'row')
      this.cateForm = row;
      this.cateOpen = true;
      this.cateTitle = "修改分类";
    },
    // 提交资讯百科分类
    submitCateForm() {
      this.$refs["cateForm"].validate(valid => {
        if (valid) {
          if (this.cateForm.id != undefined) {
            updateNewsCate(this.cateForm).then(res => {
              if (res.code == 200) {
                this.msgSuccess("修改成功");
                this.getCateList();
                this.getList();
                this.cateOpen = false;
              }
            });
          } else {
            addNewsCate(this.cateForm).then(res => {
              if (res.code == 200) {
                this.msgSuccess("保存成功");
                this.getCateList();
                this.getList();
                this.cateOpen = false;
              }
            });
          }
        }
      });
    },
    cancel1() {
      this.cateOpen = false;
      this.getCateList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    // 删除
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delNewsCate(id);
        })
        .then(() => {
          this.getCateList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },

    // 查询资讯百科文章
    getList() {
      this.loading1 = true;
      queryNews(this.queryParams).then(res => {
        console.log(res);
        this.newsList = res.rows;
        this.newsTotal = res.total;
        this.loading1 = false;
      });
    },
    getClassifyName(id) {
      let cate = this.cateList.find(item => {
        return item.id == id;
      });
      if (cate != undefined) {
        return cate.classifyName;
      }
    },
    // 点击新增
    handleAdd() {
      this.form = {
        logo: ""
      };
      this.open = true;
      this.title = "添加资讯文章";
    },
    handleUpdate(row) {
      console.log(row,'row')
      this.open = true;
      this.$nextTick(() => {
        this.form = row;
      })
      this.title = "编辑资讯文章";
    },
    handleAvatarSuccess(res) {
      this.form.logo = res.data;
    },
    cancel() {
      this.open = false;
      this.getList();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 提交资讯文章表单信息
    submitNewsForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.type = 1 
          if (this.form.id != undefined) {
            updateNews(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addNews(this.form).then(res => {
              if (res.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    handleSelectionChange1(selection) {
      this.all = selection.map(item => item.id);
      this.singleList = selection.length != 1;
      this.multipleList = !selection.length;
      console.log(this.all)
    },
    handleDeleteNews(row) {
      const id = row.id || this.all;
      console.log(id)
      this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delNews(id);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    }
  }
};
</script>

<style scoped>
.select {
  margin-left: 20px;
}
.avatar-uploader .el-upload {
  width: 148px;
  height: 148px;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 148px;
  height: 148px;
  line-height: 148px;
  text-align: center;
  border: 1px dashed #ccc;
}

.avatar {
  width: 148px;
  height: 148px;
  display: block;
}
</style>
